<template>
  <el-dialog
    title="修改用户信息"
    width="320px"
    :visible.sync="dialogVisible"
    @close="close"
  >
    <el-form ref="form" :rules="rules" :model="form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch v-model="form.state"></el-switch>
      </el-form-item>
      <el-form-item label="密码" prop="state">
        <el-input
          v-model="form.password"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="form.email"
          autocomplete="off"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="form.phone"
          autocomplete="off"
          placeholder="请输入手机号"
          minlength="11"
          maxlength="11"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="updateUserInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserInfo } from "@network/manager";
export default {
  props: {
    formData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      form: null,
      dialogVisible: true,
      rules: {
        username: [
          { trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        sex: [{ trigger: "blur", message: "请选择性别", required: true }],
        email: [
          { trigger: "blur", message: "邮箱不能为空"},
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            message: "请填写正确的邮箱",
          },
        ],
        phone: [
          { trigger: "blur", message: "手机号不能为空" },
          {
            pattern: /^1[34578]\d{9}$/, //正则校验不用字符串
            message: "请填写正确的手机号码",
            trigger: "blur",
            min: 11,
            max: 11,
          },
        ],
      },
    };
  },
  methods: {
    close() {
      this.$message.info("已取消!");
      this.$emit("close");
    },
    updateUserInfo() {
      console.log("updateState", this.form, this.$refs["form"]);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log("submit!", this.form);
          updateUserInfo(this.form)
            .then((res) => {
              if (res.data.tag) {
                this.$message.success("用户信息更新成功");
                this.$emit("close");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.$message.error("用户信息更新失败");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.form = (() => this.formData)();
  },
};
</script>

<style></style>
